package com.nanhu.test.widget.customview;

import com.nanhu.test.R;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.FontMetrics;
import android.graphics.Path;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class LabelView extends View{
	
	private Paint mTextPaint;
	
	private String mText;
	
	private int mAscent;
	
	/**
     * Constructor.  This version is only needed if you will be instantiating
     * the object manually (not from a layout XML file).
     * @param context
     */
	public LabelView(Context context){
		super(context);
		initLableView();
	}
	
	/**
     * Construct object, initializing with any attributes we understand from a
     * layout file. These attributes are defined in
     * SDK/assets/res/any/classes.xml.
     * 
     * @see android.view.View#View(android.content.Context, android.util.AttributeSet)
     */
	public LabelView(Context context, AttributeSet attrs){
		super(context, attrs);
		initLableView();
		
		TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.LabelView);
		CharSequence s = a.getString(R.styleable.LabelView_text);
		if(s != null)setText(s.toString());
		
		// Retrieve the color(s) to be used for this view and apply them.
        // Note, if you only care about supporting a single color, that you
        // can instead call a.getColor() and pass that to setTextColor().
		setTextColor(a.getColor(R.styleable.LabelView_textColor, 0xFF000000));
		
		int textSize = a.getDimensionPixelOffset(R.styleable.LabelView_textSize, 0);
		if(textSize > 0){
			setTextSize(textSize);
		}
		a.recycle();
	}
	
	private void initLableView(){
		mTextPaint = new Paint();
		mTextPaint.setAntiAlias(true);
		// Must manually scale the desired text size to match screen density
		mTextPaint.setTextSize(16);
		mTextPaint.setColor(0xFF000000);
		setPadding(3, 3, 3, 3);
	}
	
	public void setText(String text){
		mText = text;
		requestLayout();
		invalidate();
	}
	
	public void setTextSize(int size){
		// This text size has been pre-scaled by the getDimensionPixelOffset method
		mTextPaint.setTextSize(size);
		requestLayout();
		invalidate();
	}
	
	public void setTextColor(int color){
		mTextPaint.setColor(color);
		invalidate();
	}
	
	/**
     * @see android.view.View#measure(int, int)
     */
    @Override // 参数int widthMeasureSpec, int heightMeasureSpec由该view的父亲容器传进来，指示该view可以怎样显示，比如说明确指定该view应该显示的长度和高度，或者指定说，该view最多能显示的长度和高度，类型由specMode指定（MeasureSpec.EXACTLY 或MeasureSpec.AT_MOST）。
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    	setMeasuredDimension(measureWidth(widthMeasureSpec), measureHeight(heightMeasureSpec));
    }
    
    /**
     * Determines the width of this view
     * @param measureSpec A measureSpec packed into an int
     * @return The width of the view, honoring constraints from measureSpec
     */
    private int measureWidth(int measureSpec){
    	int result = 0;
    	int specMode = MeasureSpec.getMode(measureSpec);
    	int specSize = MeasureSpec.getSize(measureSpec);
    	
    	if(specMode == MeasureSpec.EXACTLY){
    		result = specSize;
    	} else {
    		result = (int)mTextPaint.measureText(mText) + getPaddingLeft() + getPaddingRight();
    		if(specMode == MeasureSpec.AT_MOST){
    			result = Math.min(result, specSize);
    		}
    	}
    	return result;
    }
    
    /**
     * Determines the height of this view
     * @param measureSpec A measureSpec packed into an int
     * @return The height of the view, honoring constraints from measureSpec
     */
    private int measureHeight(int measureSpec){
    	int result = 0;
    	int specMode = MeasureSpec.getMode(measureSpec);
    	int specSize = MeasureSpec.getSize(measureSpec);
    	
    	mAscent = (int)mTextPaint.ascent();
    	if(specMode == MeasureSpec.EXACTLY){
    		// We were told how big to be
    		result = specSize;
    	} else {
    		// Measure the text (beware: ascent is a negative number)
    		result = (int)(-mAscent + mTextPaint.descent() + getPaddingTop() + getPaddingBottom());
    		if(specMode == MeasureSpec.AT_MOST){
    			// Respect AT_MOST value if that was what is called for by measureSpec
    			result = Math.min(result, specSize);
    		}
    	}
    	return result;
    }
    
    @Override
    protected void onDraw(Canvas canvas) {
    	super.onDraw(canvas);
    	measure(0, 0);
    	
    	FontMetrics fm = mTextPaint.getFontMetrics();  
        Log.i("LabelView", "top = "+ fm.top);
        Log.i("LabelView", "bottom = "+ fm.bottom);
        Log.i("LabelView", "ascent = "+ fm.ascent);
        Log.i("LabelView", "descent = "+ fm.descent);
        Log.i("LabelView", "leading = "+ fm.leading);
    	
    	canvas.drawText(mText, getPaddingLeft(), getPaddingTop() - mAscent, mTextPaint);
    	//边框
    	mTextPaint.setStyle(Paint.Style.STROKE);
    	Path path = new Path();
    	path.moveTo(0, 0);
    	path.lineTo(getMeasuredWidth(), 0);
    	path.lineTo(getMeasuredWidth(), getMeasuredHeight());
    	path.lineTo(getMeasuredWidth(), getMeasuredHeight());
    	path.lineTo(0, getMeasuredHeight());
    	path.close();
    	canvas.drawPath(path, mTextPaint);
    	//baseline
    	canvas.drawLine(0, -fm.top, getMeasuredWidth(), -fm.top, mTextPaint);
    	//ascent
    	canvas.drawLine(0, fm.ascent - fm.top, getMeasuredWidth(), fm.ascent - fm.top, mTextPaint);
    	//descent
    	canvas.drawLine(0, fm.descent - fm.top, getMeasuredWidth(), fm.descent - fm.top, mTextPaint);
    	//top
    	//canvas.drawLine(0, fm.descent - fm.top, getMeasuredWidth(), fm.descent - fm.top, mTextPaint);
    	//bottom
    	canvas.drawLine(0, fm.bottom - fm.top, getMeasuredWidth(), fm.bottom - fm.top, mTextPaint);
    	
    }
}
